package com.hh.flow.service.impl;

import com.hh.common.utils.AssertUtil;
import com.hh.flow.constant.FlowConstant;
import com.hh.flow.domain.FlowInstance;
import com.hh.flow.dto.FlowParams;
import com.hh.flow.facade.FlowManagerService;
import com.hh.flow.mapper.FlowInstanceMapper;
import com.hh.flow.service.IFlowInstanceService;
import com.hh.mybatis.service.impl.BaseServiceImpl;
import com.hh.security.utils.SecurityUtils;
import com.hh.system.api.LoginUser;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.hh.flow.domain.tables.FlowInstanceTableDef.flow_instance;

/**
 * 流程实例Service业务层处理
 *
 * @author hh
 * @date 2023-03-29
 */
@Service
public class FlowInstanceServiceImpl extends BaseServiceImpl<FlowInstance> implements IFlowInstanceService {
    @Resource
    private FlowInstanceMapper instanceMapper;

    @Resource
    private FlowManagerService flowManagerService;

    @Override
    public FlowInstanceMapper getBaseMapper() {
        return instanceMapper;
    }

    @Override
    public QueryWrapper buildWrapper(FlowInstance flowInstance) {
        QueryWrapper query = QueryWrapper.create()
                .where(flow_instance.business_id.eq(flowInstance.getBusinessId()))
                .and(flow_instance.node_code.eq(flowInstance.getNodeCode()))
                .and(flow_instance.node_name.like(flowInstance.getNodeName()))
                .and(flow_instance.flow_status.eq(flowInstance.getFlowStatus()))
                .and(flow_instance.flow_version.eq(flowInstance.getFlowVersion()))
                .and(flow_instance.user_code.eq(flowInstance.getUserCode()))
                .and(flow_instance.user_name.like(flowInstance.getUserName()))
                .and(flow_instance.definition_id.eq(flowInstance.getDefinitionId()));
        return query;
    }

    @Override
    public List<FlowInstance> queryByidWithUpdateLock(List<Long> definitionIds) {
        AssertUtil.isFalse(definitionIds == null || definitionIds.size() == 0, FlowConstant.NOT_FOUNT_FLOW);
        for (int i = 0; i < definitionIds.size(); i++) {
            AssertUtil.isNull(definitionIds.get(i), "流程定义id不能为空!");
        }
        return instanceMapper.queryByidWithUpdateLock(definitionIds);
    }

    @Override
    public List<FlowInstance> getList(FlowInstance flowInstance) {
        return instanceMapper.getList(flowInstance);
    }

    @Override
    public Object handle(Long id, String conditionValue, String message) {
        LoginUser user = SecurityUtils.getLoginUser();
        FlowParams flowParams = new FlowParams();
        flowParams.setCreateBy(user.getUser().getCreateBy());
        flowParams.setNickName(user.getUser().getNickName());
        Long[] roleIds = user.getUser().getRoleIds();
        if (Objects.nonNull(roleIds)) {
            List<String> roleList = Arrays.stream(roleIds).map(roleId -> String.valueOf(roleId)).collect(Collectors.toList());
            flowParams.setRoles(roleList);
        }
        flowManagerService.skipFlow(id, conditionValue, message, flowParams);
        return null;
    }

}
